package org.budo.phone.freeswitch;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.budo.support.lang.util.StringUtil;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Slf4j
public class DiaplanEntity implements Serializable {
    private static final long serialVersionUID = 3131098567636777092L;

    /**
     * 指定主叫名称
     */
    public static final String origination_caller_id_name = "origination_caller_id_name";

    /**
     * 指定被叫名称
     */
    public static final String origination_callee_id_name = "origination_callee_id_name";

    public static final String absolute_codec_string = "absolute_codec_string";

    public static final String origination_caller_id_number = "origination_caller_id_number";

    private String diaplan;

    private Map<String, String> parameters;

    public String toInLine() {
        if (null == this.getParameters() || this.getParameters().isEmpty()) {
            log.error("#41 no parameters, this=" + this);
            return this.getDiaplan();
        }

        List<String> pairs = new ArrayList<String>();
        for (Entry<String, String> entry : this.getParameters().entrySet()) {
            pairs.add(entry.getKey() + "=" + entry.getValue());
        }

        return "{" + StringUtil.join(pairs, ",") + "}" + this.getDiaplan();
    }

    public DiaplanEntity setParameter(String key, String value) {
        if (null == this.getParameters()) {
            this.parameters = new HashMap<String, String>();
        }

        this.parameters.put(key, value);
        return this;
    }
}